package splay;

import static org.junit.Assert.*;
import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestSplayTree {

	private SplayTree tree;
	private static int ELEMENTS = 10;
	
	@Before
	public void setUp() throws Exception {
		tree = new SplayTreeImpl();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testHeight() {
		Assert.assertEquals(0, tree.size());
		for (int i = 0; i < ELEMENTS; i++) {
			tree.insert(i);
			Assert.assertEquals(i+1, tree.size());
			Assert.assertEquals(i, tree.height());
		}
	}

	@Test
	public void testSearch() {
		NoSplay proc = tree.search(12);
		Assert.assertEquals(true, proc.isEmpty());
		for (int i = 0; i < ELEMENTS; i++) {
			tree.insert(i);
			Assert.assertEquals(i+1, tree.size());
		}
		for (int i = ELEMENTS-1; i >=0 ; i--) {
			proc = tree.search(i);
			Assert.assertEquals(i, proc.key);
		}
	}

	@Test
	public void testInsert() {
		Assert.assertEquals(0, tree.size());
		for (int i = 0; i < ELEMENTS; i++) {
			tree.insert(i);
			Assert.assertEquals(i+1, tree.size());
		}
	}

	@Test
	public void testDelete() {
		for (int i = 0; i < ELEMENTS; i++) {
			tree.insert(i);
			Assert.assertEquals(i+1, tree.size());
		}
		for (int i = 0; i < ELEMENTS; i++) {
			tree.delete(i);
			Assert.assertEquals(ELEMENTS - i - 1, tree.size());
		}
	}

	@Test
	public void testPreOrder() {
		for (int i = 0; i < 10; i++) {
			tree.insert(i);
		}
		Assert.assertEquals("9 8 7 6 5 4 3 2 1 0 ", tree.preOrder());
	}

	@Test
	public void testSize() {
		Assert.assertEquals(0, tree.size());
		for (int i = 0; i < ELEMENTS; i++) {
			tree.insert(i);
			Assert.assertEquals(i+1, tree.size());
		}
	}

}
